clear all
%fname{1}='N:\AnalyzedData\georg\GK_120224_b\2012-03-05\GK_120224_b-Adata-19332.mat';
fname{1}='N:\AnalyzedData\georg\GK_120224_b\2012-03-09\GK_120224_b-Adata-19678.mat';
%fname{2}='N:\AnalyzedData\georg\GK_120224_b\2012-03-05\GK_120224_b-Adata-19333.mat';
%fname{3}='N:\AnalyzedData\georg\GK_120224_b\2012-03-05\GK_120224_b-Adata-19334.mat';
%fname{4}='N:\AnalyzedData\georg\GK_111206_a\2012-02-24\GK_111206_a-Adata-18777.mat';

%cells2save=[39];

% data_dir='N:\RawData\georg\';
data_dir='G:\RawData\georg\';
sess=struct;


for ind=1:length(fname)
    curr=load(fname{ind});
    sess(ind).ROIs=curr.ROIs;
    sess(ind).aux_data=[];
    %sess(ind).dx=[];
    %sess(ind).dy=[];
    
    for knd=1:length(curr.aux_files)
        tmp_data=load_lvd([data_dir curr.mouse_id '\Data\' curr.date_id '\' curr.aux_files{knd}]);
        tmp_frames=get_frame_times(tmp_data(3,:));
        
        if curr.nbr_frames(knd)~=length(tmp_frames)
            if max(diff(tmp_frames))/median(diff(tmp_frames))<1.5 & length(tmp_frames)>curr.nbr_frames(knd)
                disp('NC WARNING! Probably scanning too fast for FPGA to stop in the same frame')
                disp(['--- ' num2str(length(tmp_frames)-curr.nbr_frames(knd)) ' frames too many ---'])
                tmp_data(3,tmp_frames(curr.nbr_frames(knd)+1):end)=0;
            else
                disp('CRITICAL WARNING! - aux data longer than it should be.')
                [~,last_bad_frame]=max(diff(tmp_frames));
                tmp_data=tmp_data(:,ceil(mean([tmp_frames(last_bad_frame) tmp_frames(last_bad_frame+1)])):end);
            end
        end
        sess(ind).aux_data(:,end+1:end+length(tmp_data))=tmp_data;
        %sess(ind).dx(:,end+1:end+length(tmp_data))=curr.dx;
        %sess(ind).dy(:,end+1:end+length(tmp_data))=curr.dy;
    end
    sess(ind).frame_times=get_frame_times(sess(ind).aux_data(3,:));
end

track_pos=[];
obj_id=[];
screen_id=[];
elstim_id=[];

for ind=1:length(sess)
    track_pos(end+1:end+length(sess(ind).frame_times))=sess(ind).aux_data(9,sess(ind).frame_times);
    
% % %     obj_id(end+1:end+length(sess(ind).frame_times))=sess(ind).aux_data(10,sess(ind).frame_times);
% % %     screen_id(end+1:end+length(sess(ind).frame_times))=sess(ind).aux_data(11,sess(ind).frame_times);
% % %     elstim_id(end+1:end+length(sess(ind).frame_times))=sess(ind).aux_data(12,sess(ind).frame_times);
end

cell_num=[1:length(sess(1).ROIs)];
act=[];
activity_matrix=zeros(length(cell_num),length(track_pos));
for j=1:length(cell_num)
    cnt=0;
    for lnd=1:length(sess)
        %act=ftfil(sess(lnd).ROIs(cell_num(j)).activity,80,0.1,20);
        act=sess(lnd).ROIs(cell_num(j)).activity;
% % %         if lnd==1
% % %             act(length(act))=[];
% % %         end
% % %         if lnd==2
% % %             act(length(act))=[];
% % %         end
% % %         if lnd==3
% % %             act(length(act))=[];
% % %         end

% % %         act=psmooth(act);
        raw_act=act/median(act);
        activity_matrix(j,cnt+1:cnt+length(sess(lnd).frame_times))=raw_act;
        cnt=cnt+length(sess(lnd).frame_times);
    end
end

maxlim=max(track_pos);
minlim=min(track_pos);

last_turn=1;
turn_u=0;
turn_d=0;
refract=0;
dir_ind=zeros(length(track_pos),1);
last_turn_dir=NaN;
    
for ind=1:length(track_pos)

    if refract==1
        if abs(track_pos(ind))<1
            refract=0;
        end
    end

    if refract==0
        if track_pos(ind)<minlim+0.1
            turn_u=1;
            refract=1;
        elseif track_pos(ind)>maxlim-0.1
            turn_d=1;
            refract=1;
        end
    end

    if turn_u
        dir_ind(last_turn:ind)=1;
        turn_u=0;
        last_turn=ind;
        last_turn_dir=1;
    elseif turn_d
        turn_d=0;
        last_turn=ind;
        last_turn_dir=0;
    end
end

if last_turn_dir==0
    dir_ind(last_turn:end)=1;
end

dir_ind=logical(dir_ind);
dir_ind_ft=dir_ind;
% % %dir_ind_ft=dir_ind(frame_times);

% % % figure;plot(track_pos)
% % % hold on
% % % plot(dir_ind_ft)

change_pos=[];
dnt=1;

change_pos(1)=1;

for ond=1:(length(dir_ind_ft)-1)
    if dir_ind_ft(ond)~=dir_ind_ft(ond+1);
        dnt=dnt+1;
        change_pos(dnt)=ond+1;
    end
end

change_pos(end+1)=length(dir_ind_ft);

nbins=100;
bins=[minlim:(maxlim-minlim)/nbins:maxlim];

trav_dir=zeros(length(change_pos)-1,1);
act_overlay=nan(nbins,length(change_pos)-1,length(cell_num));
% % % obj_id_dir=[];
screen_id_dir=[];
% % % obj_pos=[];
% % % elstim_id_dir=[];

% % % screen_id_new(1:3515)=1;
% % % screen_id_new(3516:5423)=0;
% % % screen_id_new(5424:9508)=2;
% % % screen_id_new(9509:10245)=0;
% % % screen_id_new(10246:14453)=1;
% % % screen_id_new(14454:15191)=0;
% % % screen_id_new(15192:19526)=2;
% % % screen_id_new(19527:20329)=0;
% % % screen_id_new(20330:24512)=1;
% % % screen_id_new(24513:25148)=0;
% % % screen_id_new(25149:29518)=2;
% % % screen_id_new(29519:30295)=0;
% % % screen_id_new(30295:34500)=1;
% % % screen_id_new(34500:35164)=0;
% % % screen_id_new(35165:length(screen_id))=2;

for ind=1:(length(change_pos)-1)
    act_div=zeros(length(dir_ind_ft),1);
    act_div(change_pos(ind):change_pos(ind+1))=1;
    for knd=1:nbins
        act_overlay(knd,ind,:)=mean(activity_matrix(:,track_pos>=bins(knd)&track_pos<bins(knd+1)&act_div'==1),2);
    end
    if dir_ind_ft(change_pos(ind))==1;
        trav_dir(ind)=1;
    end
% % %     elstim_id_dir(ind)=max(elstim_id(change_pos(ind):change_pos(ind+1)));
% % %     obj_id_dir(ind)=round(mode(obj_id(change_pos(ind):change_pos(ind+1))));
% % %     if obj_id_dir(ind)>0
% % %         if obj_id_dir(ind-1)==0
% % %             obj_pos(ind)=1;
% % %         else
% % %             obj_pos(ind)=0;
% % %         end
% % %     else
% % %         obj_pos(ind)=0;
% % %     end
% % %     screen_id_dir(ind)=mode(screen_id_new(change_pos(ind):change_pos(ind+1)));
% % %     screen_id_dir(ind)=mode(screen_id(change_pos(ind):change_pos(ind+1)));
end

% % % if length(unique(obj_id_dir))>2
% % %     x=unique(obj_id_dir);
% % % % % %     obj_id_dir(obj_id_dir==3)=2;
% % %     mul_obj=[];
% % %     cue_1=obj_id_dir;
% % %     cue_1(cue_1==2)=1;
% % %     cue_2=obj_id_dir;
% % %     cue_2(cue_2==1)=0;
% % %     cue_2(cue_2==2)=1;
% % %     cue_2(cue_2==3)=1;
% % %     mul_obj(:,1)=cue_1;
% % %     mul_obj(:,2)=cue_2;
% % %     obj_axis=0.02*(length(x)-1);
% % %     obj_id_dir_lt=[];
% % %     obj_id_dir_lt(:,1)=mul_obj(trav_dir==0,1);
% % %     obj_id_dir_lt(:,2)=mul_obj(trav_dir==0,2);
% % %     obj_id_dir_rt=[];
% % %     obj_id_dir_rt(:,1)=mul_obj(trav_dir==1,1);
% % %     obj_id_dir_rt(:,2)=mul_obj(trav_dir==1,2);
% % %     
% % %     obj_pos_lt=obj_pos(trav_dir==0);
% % %     obj_pos_rt=obj_pos(trav_dir==1);
% % % else
% % %     obj_axis=0.02;
% % %     obj_id_dir_lt=obj_id_dir(trav_dir==0)';
% % %     obj_id_dir_rt=obj_id_dir(trav_dir==1)';
% % %     obj_pos_lt=obj_pos(trav_dir==0);
% % %     obj_pos_rt=obj_pos(trav_dir==1);    
% % %     
% % % end

act_overlay_lt=act_overlay(:,trav_dir==0,:);
act_overlay_lt(isnan(act_overlay_lt))=1;

% % % screen_id_dir_lt=screen_id_dir(trav_dir==0);
% % % screen_id_dir_rt=screen_id_dir(trav_dir==1);

act_overlay_rt=act_overlay(:,trav_dir==1,:);
act_overlay_rt(isnan(act_overlay_rt))=1;



limit_calc=act_overlay;
limit_calc(isnan(limit_calc))=1;
max_act=[];
min_act=[];
sat_amt=[];
for gnd=1:length(cell_num)
    std_act=std2(limit_calc(:,:,gnd));
    median_act=median(limit_calc(:,:,gnd));
    median_act=median(median_act);
    max_act(gnd)=median_act+(std_act*3);
    min_act(gnd)=median_act-(std_act*0.5);
% % %     mean_act=mean2(limit_calc(:,:,gnd));
% % %     max_act(gnd)=mean_act+(std_act*3);
% % %     min_act(gnd)=mean_act-(std_act*0.5);


% % %     btm_lim=round((mean_act-(std_act*3))*100);
% % %     top_lim=round((mean_act+(std_act*3))*100);


% % %     btm_lim=round((median_act-(std_act*0.5))*100);
% % %     top_lim=round((median_act+(std_act*3))*100);
% % %     x=round(limit_calc(:,:,gnd)*100);
% % %     y=round(unique(x));
% % %     mat=[];
% % %     dnt=1;
% % %     for ind=1:length(y)
% % %         cnt=x(x==y(ind));
% % %         mat(ind)=length(cnt);
% % %         if dnt==1
% % %             if y(ind)>btm_lim
% % %                 btm_lim_point=ind-1;
% % %                 dnt=2;
% % %             end
% % %         elseif dnt==2
% % %             if y(ind)>top_lim
% % %                 top_lim_point=ind;
% % %                 dnt=3;
% % %             end
% % %         end
% % %     end
% % %     if btm_lim<y(1)
% % %         top_lim_sum=sum(mat(top_lim_point:end));
% % %         sat_amt(gnd)=(top_lim_sum/sum(mat))*100;
% % %     else
% % %         btm_lim_sum=sum(mat(1:btm_lim_point));
% % %         top_lim_sum=sum(mat(top_lim_point:end));
% % %         sat_amt(gnd)=((btm_lim_sum+top_lim_sum)/sum(mat))*100;
% % %     end
% % %     figure
% % %     plot(y,mat)
% % %     ylim=get(gca,'ylim');
% % %     line([mean_act*100 mean_act*100],ylim,'color','r')
% % %     line([btm_lim btm_lim],ylim,'color','black')
% % %     line([top_lim top_lim],ylim,'color','black')
% % %     title(strcat(num2str(sat_amt(gnd)),' ',num2str(cell_num(gnd))))
end

% % % if length(unique(obj_id_dir))==2
% % %     obj_pos_1a=(100/16)*4;
% % %     obj_pos_1b=(100/16)*5;
% % %     cue=1;
% % % end
% % % if length(unique(obj_id_dir))==3
% % %     obj_pos_1a=(100/18)*4.5;
% % %     obj_pos_1b=(100/18)*5.5;
% % %     obj_pos_2a=(100/16)*10;
% % %     obj_pos_2b=(100/16)*11;
% % %     cue=2;
% % % end
% % % 
% % % trav_num_lt=find(obj_pos_lt);
% % % trav_num_rt=find(obj_pos_rt);
% % % 

% % % elstim_id_dir_lt=elstim_id_dir(trav_dir==0);
% % % elstim_id_dir_rt=elstim_id_dir(trav_dir==1);
mkdir('C:\Users\zmarz\Desktop\LEC_results',[curr.mouse_id,'\',curr.date_id]);
for und=1:length(cell_num)
% % % for und=1:length(cells2save)
    
    figure('color','black')%,'Visible','off')
    
% % %     axes('position',[0.09 0.505 obj_axis 0.45])
% % %     imagesc(obj_id_dir_lt)
% % %     text(0.5,-7,'Cue','color','white')
% % %     axis off
% % %     box off
% % %     if cue==2
% % %         text(0.5,-0.5,'1','FontWeight','bold','color','white')
% % %         text(1.5,-0.5,'2','FontWeight','bold','color','white')
% % %     end
% % %     axes('position',[0.09 0.01 obj_axis 0.45])
% % %     imagesc(obj_id_dir_rt)
% % %     axis off
% % %     box off
    

% % %     axes('position',[0.09 0.505 0.02 0.45])
% % %     imagesc(elstim_id_dir_lt')
% % %     text(0.5,-7,'Stim','color','white')
% % %     axis off
% % %     box off
% % % 
% % %     axes('position',[0.09 0.01 0.02 0.45])
% % %     imagesc(elstim_id_dir_rt')
% % %     axis off
% % %     box off

    axes('position',[0.15 0.505 0.85 0.45])
    imagesc(act_overlay_lt(:,:,und)')
    colorbar
    set(gca,'clim',[min_act(und) max_act(und)])
    col=get(gca,'clim');
    title(regexprep(strcat(regexprep(curr.mouse_id,'_',''),'_',curr.date_id,'_','Cell:','_',num2str(cell_num(und))),'_',' '),'FontWeight','bold','FontSize',14,'color','white')
    axis off
% % %     imagesc(act_overlay_lt(:,:,cells2save(und))')
% % %     title(regexprep(strcat(regexprep(curr.mouse_id,'_',''),'_',curr.date_id,'_','Cell:','_',num2str(cells2save(und))),'_',' '),'FontWeight','bold','FontSize',14)
% % %     axis off
% % %     box off
% % %     if cue==1
% % %         line([obj_pos_1a obj_pos_1a],[0 size(act_overlay_lt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         line([obj_pos_1b obj_pos_1b],[0 size(act_overlay_lt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         if ~isempty(trav_num_lt)
% % %             for tnd=1:length(trav_num_lt)
% % %                 text(0,trav_num_lt(tnd),'*','color','white')
% % %             end
% % %         end
% % %     end
% % %     if cue==2
% % %         line([obj_pos_1a obj_pos_1a],[0 size(act_overlay_lt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         line([obj_pos_1b obj_pos_1b],[0 size(act_overlay_lt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         line([obj_pos_2a obj_pos_2a],[0 size(act_overlay_lt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         line([obj_pos_2b obj_pos_2b],[0 size(act_overlay_lt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         if size(trav_num_lt,2)==1
% % %             if trav_num_lt>trav_num_rt
% % %                 text(0,trav_num_lt,'*','color','white')
% % %             else
% % %                 text(0,trav_num_lt,'*','color','white')
% % %             end
% % %         elseif size(trav_num_lt,2)==2
% % %             if trav_num_lt(1)>trav_num_lt(2)
% % %                 text(0,trav_num_lt(2),'*','color','white')
% % %                 text(0,trav_num_lt(1),'*','color','white')
% % %             else
% % %                 text(0,trav_num_lt(1),'*','color','white')
% % %                 text(0,trav_num_lt(2),'*','color','white')
% % %             end
% % %         end
% % %     end

% % %     set(gca,'clim',[min_act(cells2save(und)) max_act(cells2save(und))])

    axes('position',[0.15 0.01 0.85 0.45])
    imagesc(act_overlay_rt(:,:,und)')
% % %     imagesc(act_overlay_rt(:,:,cells2save(und))')
    colorbar
    set(gca,'clim',col)
    title('down traversal','color','white')
    axis off
    box off
% % %     if cue==1
% % %         line([obj_pos_1a obj_pos_1a],[0 size(act_overlay_rt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         line([obj_pos_1b obj_pos_1b],[0 size(act_overlay_rt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         text(obj_pos_1a,0,'Cue1','color','white')
% % %         if ~isempty(trav_num_rt)
% % %             for fnd=1:length(trav_num_rt)
% % %                 text(0,trav_num_rt(fnd),'*','color','white')
% % %             end
% % %         end
% % %     end
% % %     if cue==2
% % %         line([obj_pos_1a obj_pos_1a],[0 size(act_overlay_rt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         line([obj_pos_1b obj_pos_1b],[0 size(act_overlay_rt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         line([obj_pos_2a obj_pos_2a],[0 size(act_overlay_rt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         line([obj_pos_2b obj_pos_2b],[0 size(act_overlay_rt,2)],'LineWidth',2,'LineStyle','--','color','black')
% % %         text(obj_pos_1a,0,'Cue1','color','white')
% % %         text(obj_pos_2a,0,'Cue2','color','white')
% % %         if size(trav_num_rt,2)==1
% % %             if trav_num_rt>trav_num_lt
% % %                 text(0,trav_num_rt,'*','color','white')
% % %             else
% % %                 text(0,trav_num_rt,'*','color','white')
% % %             end
% % %         elseif size(trav_num_rt,2)==2
% % %             if trav_num_rt(1)>trav_num_rt(2)
% % %                 text(0,trav_num_rt(2),'*','color','white')
% % %                 text(0,trav_num_rt(1),'*','color','white')
% % %             else
% % %                 text(0,trav_num_rt(1),'*','color','white')
% % %                 text(0,trav_num_rt(2),'*','color','white')
% % %             end
% % %         end
% % %     end
% % %     axes('position',[0.03 0.505 0.02 0.45])
% % %     imagesc(screen_id_dir_lt')
% % %     title('Screen','color','white')
% % %     axis off
% % %     box off
% % %     
% % %     axes('position',[0.03 0.01 0.02 0.45])
% % %     imagesc(screen_id_dir_rt')
% % %     axis off
% % %     box off
    
% % %     set(gcf,'PaperOrientation','landscape','PaperUnits','normalized','PaperPosition', [0.05 0.05 0.9 0.9])
% % %     print(gcf,'-P\\s10\Babylon')
    filename=strcat('C:\Users\zmarz\Desktop\LEC_results\',curr.mouse_id,'\',curr.date_id,'\',num2str(cell_num(und)));
% % %     filename=strcat('C:\Users\zmarz\Desktop\LEC_results\',curr.mouse_id,'\',curr.date_id,'\',num2str(cell_num(und)));
    set(gcf, 'InvertHardCopy', 'off');
    print(gcf,'-dbmp',filename)
end

cnt=0;
num_fig_mat=[];
g=1;
for n=1:length(cell_num)
    cnt=cnt+1;
    num_fig_mat(n,g)=cell_num(n);
    if cnt==10
        cnt=0;
        g=g+1;
    end
end

num_fig=size(num_fig_mat,2);
for z=1:num_fig
    cells=unique(num_fig_mat(:,z));
    cells(cells==0)=[];
    figure('color','white')%,'Visible','off')
    for m=1:length(cells)
        axes('position',[0.02 0.9-(0.1*(m-1)) 0.95 0.1])
        hold on
        plot(activity_matrix(cells(m),:),'color','black')
        axis off
        text(size(activity_matrix,2),0.9-(0.1*(m-1)),num2str(cells(m)),'FontWeight','bold','color','blue')
% % %         if m==length(cells)
% % %             axes('position',[0.02 0 0.95 0.2])
% % %             hold on
% % %             plot(track_pos,'LineWidth',2,'color','yellow')
% % %             axis off
% % %         end
    end
    %set(gcf,'PaperOrientation','landscape','PaperUnits','normalized','PaperPosition', [0.05 0.05 0.9 0.9])
    %print(gcf,'-P\\s10\Babylon')
    filename2=strcat('C:\Users\zmarz\Desktop\LEC_results\',curr.mouse_id,'\',curr.date_id,'\','track_pos','_',num2str(z),'b');
    print(gcf,'-dbmp',filename2)
end


% % % max_lim=size(activity_matrix,2);
% % % max_lim_s=(max_lim*12.5)/1000;
% % % figure('color','white')%,'Visible','off')
% % % for m=1:length(cell_num)
% % %     axes('position',[0.02 0.9-(0.1*(m-1)) 0.95 0.1])
% % %     hold on
% % %     plot(activity_matrix(cell_num(m),:),'color','black')
% % %     axis off
% % %     text(size(activity_matrix,2),0.9-(0.1*(m-1)),num2str(cell_num(m)),'FontWeight','bold','color','blue')
% % %     if m==1
% % %         hold on
% % %         text(size(activity_matrix,2),0.9,num2str(cell_num(m)),'FontWeight','bold','color','blue')
% % %    
% % %         axes('position',[0.02 0.9-(0.1*(m)) 0.95 0.1],'ycolor',get(gcf,'color'),'ytick',[])
% % %         hold on
% % %         plot(track_pos,'LineWidth',2,'color','black')
% % %         set(gca,'xtick',[0:40000:max_lim],'xticklabel',[0:500:max_lim_s])
% % %         xlabel('Time(s)')
% % %     end
% % % end
% % % % % % set(gcf,'PaperOrientation','landscape','PaperUnits','normalized','PaperPosition', [0.05 0.05 0.9 0.9])
% % % print(gcf,'-P\\s10\Babylon')
% % % filename2=strcat('C:\Users\zmarz\Desktop\LEC_results\',curr.mouse_id,'\',curr.date_id,'\','track_pos');
% % % print(gcf,'-dbmp',filename2)

act_overlay_pop=[];
for ind=1:(length(change_pos)-1)
    act_div=zeros(length(dir_ind_ft),1);
    act_div(change_pos(ind):change_pos(ind+1))=1;
    for knd=1:nbins
        act_overlay_pop(knd,ind)=mean2(activity_matrix(:,track_pos>=bins(knd)&track_pos<bins(knd+1)&act_div'==1));
    end
end

act_overlay_pop_lt=act_overlay_pop(:,trav_dir==0,:);
act_overlay_pop_lt(isnan(act_overlay_pop_lt))=1;
act_overlay_pop_rt=act_overlay_pop(:,trav_dir==1,:);
act_overlay_pop_rt(isnan(act_overlay_pop_rt))=1;

limit_calc_pop=act_overlay_pop;
limit_calc_pop(isnan(limit_calc_pop))=1;

std_act_pop=std2(limit_calc_pop);
median_act_pop=median(limit_calc_pop);
median_act_pop=median(median_act_pop);
max_act_pop=median_act_pop+(std_act_pop*3);
min_act_pop=median_act_pop-(std_act_pop*0.5);

%mat_num_3=1
%mat_pop

% % % 
figure;axes('position',[0.1 0.505 0.02 0.45])
% % % imagesc(obj_id_dir_lt')
% % % title 'Object'
% % % axis off
% % % box off

axes('position',[0.15 0.505 0.85 0.45])
imagesc(act_overlay_pop_lt(:,:)')
title(strcat(regexprep(curr.mouse_id,'_',' '),' ','Population activity per traversal'))
axis off
box off
colorbar
set(gca,'clim',[min_act_pop max_act_pop])
col=get(gca,'clim');

% % % axes('position',[0.1 0.01 0.02 0.45])
% % % imagesc(obj_id_dir_rt')
% % % axis off
% % % box off

axes('position',[0.15 0.01 0.85 0.45])
imagesc(act_overlay_pop_rt(:,:)')
title 'down traversal'
axis off
box off
colorbar
set(gca,'clim',col)

% % % axes('position',[0.05 0.505 0.02 0.45])
% % % imagesc(screen_id_dir_lt')
% % % title 'Screen'
% % % axis off
% % % box off
% % % 
% % % axes('position',[0.05 0.01 0.02 0.45])
% % % imagesc(screen_id_dir_rt')
% % % axis off
% % % box off

% totalup=act_overlay_pop_lt;
% totaldown=act_overlay_pop_rt;
% totalup=totalup/max(totalup(:));
% totaldown=totaldown/max(totaldown(:));
% for i = 1:size(totalup,1)
%     [ua,ub]=findpeaks(smooth(totalup(i,:),10));
%     [da,db]=findpeaks(smooth(totaldown(i,:),10));
%     utemp(i)=ub(ua==max(ua));
%     dtemp(i)=db(da==max(da));
% end
% [uaa,ubb]=sort(utemp);
% [daa,dbb]=sort(dtemp);
% totalup=totalup(ubb,:);
% totaldown=totaldown(dbb,:);
% figure(5)
% imagesc(totalup')
% figure(6)
% plot(sum(totalup,1)/length(totalup))
% figure(7)
% imagesc(totaldown')
% figure(8)
% plot(sum(totaldown,1)/length(totaldown))
% 
% nbins=32;
% bins=[minlim:(maxlim-minlim)/nbins:maxlim];
% 
% cell_num=[13];
% totalup=[];
% totaldown=[];
% for ind=1:length(cell_num)
%     act_n=[];
%     for knd=1:nbins
%         act_n(knd,1)=mean(activity_matrix(cell_num(ind),track_pos>=bins(knd)&track_pos<bins(knd+1)&dir_ind_ft'==1));
%         act_n(knd,2)=mean(activity_matrix(cell_num(ind),track_pos>=bins(knd)&track_pos<bins(knd+1)&dir_ind_ft'==0));
%     end
%     %figure;imagesc([act_n(:,1) act_n(:,2)]')
%     %colorbar
%     totalup(ind,:)=act_n(:,1);
%     totaldown(ind,:)=act_n(:,2);
% 
% end
% totalup=totalup/max(totalup(:));
% totaldown=totaldown/max(totaldown(:));
% utemp=[];
% dtemp=[];
% for i = 1:size(totalup,1)
%     [ua,ub]=findpeaks(smooth(totalup(i,:),10));
%     [da,db]=findpeaks(smooth(totaldown(i,:),10));
%     utemp(i)=ub(ua==max(ua));
%     dtemp(i)=db(da==max(da));
% end
% [uaa,ubb]=sort(utemp);
% [daa,dbb]=sort(dtemp);
% totalup=totalup(ubb,:);
% totaldown=totaldown(dbb,:);
% figure(5)
% imagesc(totalup)
% figure(6)
% plot(sum(totalup,1)/length(totalup))
% figure(7)
% imagesc(totaldown)
% figure(8)
% plot(sum(totaldown,1)/length(totaldown))